//
//  RetrievePasswordViewController.m
//  Guji
//
//  Created by 华谕 on 2017/9/5.
//  Copyright © 2017年 LXF. All rights reserved.
//

#import "RetrievePasswordViewController.h"

static int seconds = 60;//计时60s
@interface RetrievePasswordViewController ()<UITextFieldDelegate>
{
    UIView *backView;
    UITextField *phoneField;
    UITextField *passwordField;
    UITextField *codeField;
    UIButton *codeBtn;
    NSTimer *timer;
}


@end

@implementation RetrievePasswordViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jianpanhuishou:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    //    [timer invalidate];
    [self renewTimer];
    timer = nil;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
    
}

- (void)creatUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = _titleString;
    self.navigationController.navigationBar.translucent = NO;
    backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT)];
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    NSArray *titleAry = @[@"手机",@"密码",@"验证码"];
    NSArray *imgAry = @[@"logphone",@"logPassw",@"codeimg"];
    NSArray *pladerAry = @[@"在这里输入手机号",@"请输入密码",@"请输入验证码"];
    for (int i = 0; i < titleAry.count; i ++)
    {
        UIImageView *imgView = [ZCControl createImageViewWithFrame:CGRectMake(35*displaywidth,65*displaywidth+95*displaywidth*i,20*displaywidth,25*displaywidth) ImageName:imgAry[i]];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        [backView addSubview:imgView];
        UILabel *titLab = [ZCControl createLabelWithFrame:CGRectMake(65*displaywidth, 65*displaywidth+95*displaywidth*i, 70*displaywidth, 25*displaywidth) Font:13.0 Text:titleAry[i] Color:RGB(77, 77, 77)];
        [backView addSubview:titLab];
        UITextField *textfield = [ZCControl createTextFieldWithFrame:CGRectMake(50*displaywidth, 100*displaywidth+95*displaywidth*i, SCREEN_WIDTH-100*displaywidth, 30*displaywidth) placeholder:pladerAry[i] passWord:NO leftImageView:nil rightImageView:nil Font:13.0];
        [textfield setValue:RGB(179, 179, 179) forKeyPath:@"_placeholderLabel.textColor"];
        textfield.textColor = RGB(77, 77, 77);
        textfield.delegate = self;
        [backView addSubview:textfield];
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(35*displaywidth, 140*displaywidth+95*displaywidth*i, SCREEN_WIDTH-70*displaywidth, 1*displaywidth)];
        line.backgroundColor = LinesColor;
        [backView addSubview:line];
        if (i == 0)
        {
            textfield.keyboardType = UIKeyboardTypeNumberPad;
            if ([_titleString isEqualToString:@"修改密码"])
            {
                textfield.text = [LTools cacheForKey:SERVER_Phone];
            }
//            textfield.userInteractionEnabled = NO;
            phoneField = textfield;
        }
        else if (i == 1)
        {
            textfield.secureTextEntry = YES;
            passwordField = textfield;
        }
        else
        {
            textfield.keyboardType = UIKeyboardTypeNumberPad;
            textfield.frame = CGRectMake(50*displaywidth, 100*displaywidth+95*displaywidth*i, 120*displaywidth, 30*displaywidth);
            codeField = textfield;
            codeBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH-160*displaywidth, 100*displaywidth+95*displaywidth*i, 120*displaywidth, 30*displaywidth) ImageName:nil Target:self Action:@selector(verifiBtnAction:) Title:@"获取验证码" Font:13.0];
            [codeBtn setTitleColor:RGB(77, 77, 77) forState:UIControlStateNormal];
            codeBtn.backgroundColor = [UIColor whiteColor];
            [codeBtn.layer setCornerRadius:15*displaywidth];
            [codeBtn.layer setMasksToBounds:YES];
            codeBtn.layer.borderColor = ThemeColor.CGColor;
            codeBtn.layer.borderWidth = 1*displaywidth;
            [backView addSubview:codeBtn];
        }
        
    }
    UIButton *soureBtn = [ZCControl createButtonWithFrame:CGRectMake(60*displaywidth , 365*displaywidth, SCREEN_WIDTH-120*displaywidth, 45*displaywidth) ImageName:nil Target:self Action:@selector(soureAction) Title:@"确认" Font:15.0];
    [soureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    soureBtn.backgroundColor = ThemeColor;
    [soureBtn.layer setMasksToBounds:YES];
    [soureBtn.layer setCornerRadius:5*displaywidth];
    [self.view addSubview:soureBtn];
   
    
}


- (void)verifiBtnAction:(UIButton *)btn
{
    if (phoneField.text.length < 1)
    {
        [LTools showMBProgressWithText:@"请输入手机号" addToView:self.view];
        return;
    }
    if (phoneField.text.length != 11)
    {
        [LTools showMBProgressWithText:@"请输入正确的手机号" addToView:self.view];
        return;
    }
    [self requestRegisCode4register];
    
}

//验证码
- (void)requestRegisCode4register
{
    [self.view endEditing:YES];
    WEAKSELF;
    NSDictionary *dic = @{@"phone":phoneField.text,
                          @"type":@"2"
                          };
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    [HYBaseHandler requestCodeRegisterParmers:dic Success:^(id obj)
//     {
//         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
//         if ([obj server_Getcodecheck])
//         {
//             [LTools showMBProgressWithText:@"验证码已发送" addToView:weakSelf.view];
//             [weakSelf startTimer];
//         }
//         else
//         {
//             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
//         }
//     } failed:^(NSError *error) {
//         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
//
//     }];
}

//确认
- (void)soureAction
{
    [self.view endEditing:YES];
    if (phoneField.text.length != 11)
    {
        [LTools showMBProgressWithText:@"请输入手机号码" addToView:self.view];
        return;
    }
    if ([passwordField.text isEqualToString:@""])
    {
        [LTools showMBProgressWithText:@"请设置密码" addToView:self.view];
        return;
    }
    if ([codeField.text isEqualToString:@""])
    {
        [LTools showMBProgressWithText:@"请输入验证码" addToView:self.view];
        return;
    }
    [self requestPublicKey:passwordField.text];
}

- (void)requestPublicKey:(NSString *)passWorld
{
    WEAKSELF;
    [MBProgressHUD showHUDAddedTo:sharedDelegate.window animated:YES];
//    [HYBaseHandler requestPublicKeyParmers:nil Success:^(id obj)
//     {
//         if ([obj server_Getcodecheck])
//         {
//             NSString *publicKey = obj[@"data"];
//             //测试要加密的数据
//             NSString *sourceStr = passWorld;
//             //公钥加密
//             NSString *encryptStr = [RSA encryptString:sourceStr publicKey:publicKey];
//             NSDictionary *dic = @{@"phone":phoneField.text,
//                                   @"password":encryptStr,
//                                   @"msgCode":codeField.text,
//                                   @"type":@"2"
//                                   };
//             [self requestRegisterParmers:dic];
//         }
//         else
//         {
//             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
//             [MBProgressHUD hideAllHUDsForView:sharedDelegate.window animated:YES];
//         }
//     } failed:^(NSError *error) {
//         [MBProgressHUD hideAllHUDsForView:sharedDelegate.window animated:YES];
//     }];
}

- (void)requestRegisterParmers:(NSDictionary *)dic
{
    WEAKSELF;
//    [HYBaseHandler requestUpdatePswParmers:dic Success:^(id obj)
//     {
//         [MBProgressHUD hideAllHUDsForView:sharedDelegate.window animated:YES];
//         if ([obj server_Getcodecheck])
//         {
//             [LTools showMBProgressWithText:@"操作成功" addToView:sharedDelegate.window];
//             [weakSelf.navigationController popViewControllerAnimated:YES];
//             
//         }
//         else
//         {
//             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
//         }
//         
//     } failed:^(NSError *error) {
//         
//     }];
}

- (void)startTimer
{
    seconds = 60;
    
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(calculateTime) userInfo:Nil repeats:YES];
    codeBtn.userInteractionEnabled = NO;
}
//计算时间
- (void)calculateTime
{
    NSString *title = [NSString stringWithFormat:@"(%dS)重新获取",seconds];
    codeBtn.titleLabel.text = title;
    [codeBtn setTitle:title forState:UIControlStateNormal];
    
    if (seconds != 0) {
        seconds --;
    }else
    {
        [self renewTimer];
    }
    
}
//计时器归零
- (void)renewTimer
{
    [timer invalidate];//计时器停止
    codeBtn.userInteractionEnabled = YES;
    [codeBtn setTitle:@"重新获取验证码" forState:UIControlStateNormal];
    seconds = 60;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == codeField)
    {
        backView.frame = CGRectMake(0, -160*displaywidth, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT);
        
    }
}

- (void)scrollTap:(id)sender
{
    [self.view endEditing:YES];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField == phoneField && [_titleString isEqualToString:@"修改密码"])
    {
        return NO;
    }
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView
{
    [self.view endEditing:YES];
}

- (void)jianpanhuishou:(NSNotification *)notification
{
    backView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
